package com.example.administrator.express.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.express.R;
import com.example.administrator.express.http.HttpClient;
import com.example.administrator.express.http.HttpResponseHandler;
import com.example.administrator.express.model.Packageinfo;
import com.google.gson.Gson;

import java.io.IOException;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.FormBody;
import okhttp3.Request;

/**
 * Created by Administrator on 2016/9/18.
 */
public class PackageActivity extends Activity {
    @Bind(R.id.btnBack)
    Button btnBack;

    @Bind(R.id.receivetime)
    TextView receivetime;
    @Bind(R.id.receiveperson)
    TextView receiveperson;

    @Bind(R.id.view2)
    View view2;
    @Bind(R.id.deliverytime)
    TextView deliverytime;
    @Bind(R.id.deliveryperson)
    TextView deliveryperson;
    @Bind(R.id.view2right)
    View view2right;
    @Bind(R.id.view2left)
    View view2left;
    @Bind(R.id.view3)
    View view3;
    @Bind(R.id.view33)
    View view33;
    @Bind(R.id.signtime)
    TextView signtime;
    @Bind(R.id.signperson)
    TextView signperson;
    @Bind(R.id.package_sn)
    TextView packageSn;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_packageinfo);
        ButterKnife.bind(this);
        initdata();
        btnBack.setVisibility(View.VISIBLE);

    }

    public void initdata() {
        Integer snid = getIntent().getIntExtra("sn", 0);
        postpackage(snid, "getpackagebysn");

    }

    public void postpackage(final Integer snid, String action) {
        try {
            String url = "http://oa24406206.carp.mopaasapp.com/";
            url = url + action;
            FormBody formBody = new FormBody.Builder()
                    .add("sn", String.valueOf(snid))
                    .build();

            HttpClient.post(url, formBody, new HttpResponseHandler() {
                @Override
                public void onSuccess(int statusCode, String content) {

                    System.out.println("成功:" + content);
                    Gson gson = new Gson();
                    Packageinfo packageinfo = gson.fromJson(content, Packageinfo.class);
                    if (packageinfo != null) {
                        packageSn.setText(String.valueOf(packageinfo.getSn()));
                        if (packageinfo.getStatus() == 1) {
                            receivetime.setText(packageinfo.getReceiveTime());
                            receiveperson.setText("揽收人:" + packageinfo.getReceiverId());

                        } else if (packageinfo.getStatus() == 2) {
                            view2.setBackgroundResource(R.drawable.common_circle_orange);
                            view2left.setBackgroundColor(getResources().getColor(R.color.orange));
                            view2right.setBackgroundColor(getResources().getColor(R.color.orange));
                            receivetime.setText(packageinfo.getReceiveTime());
                            receiveperson.setText("揽收人:" + packageinfo.getReceiverId());
                            deliverytime.setText(packageinfo.getDeliveryTime());
                            deliveryperson.setText("派送人:" + packageinfo.getDeliveryId());
                        } else {
                            view2.setBackgroundResource(R.drawable.common_circle_orange);
                            view2left.setBackgroundColor(getResources().getColor(R.color.orange));
                            view2right.setBackgroundColor(getResources().getColor(R.color.orange));
                            receivetime.setText(packageinfo.getReceiveTime());
                            receiveperson.setText("揽收人:" + packageinfo.getReceiverId());
                            deliverytime.setText(packageinfo.getDeliveryTime());
                            deliveryperson.setText("派送人:" + packageinfo.getDeliveryId());
                            view3.setBackgroundResource(R.drawable.common_circle_orange);
                            view33.setBackgroundColor(getResources().getColor(R.color.orange));
                            signperson.setText("签收人:" + packageinfo.getSignId());
                            signtime.setText(packageinfo.getSignTime());
                        }
                    } else {
                        Toast.makeText(PackageActivity.this, "此单号不存在", Toast.LENGTH_SHORT).show();
                    }


                }

                @Override
                public void onFailure(Request request, IOException e) {
                    super.onFailure(request, e);
                    Toast.makeText(PackageActivity.this, "链接服务器失败", Toast.LENGTH_SHORT).show();
                    System.out.println("插入失败:");
                }
            });


        } catch (Exception e) {
            e.printStackTrace();
        }


    }


    @OnClick(R.id.btnBack)
    public void onClick() {
        this.finish();
    }
}
